package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetDishMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetDishMapper setDishMapper;

    /**
     *添加购物车
     * @param shoppingCartDTO
     */

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());//当前用户
        //1.首先要判断这个购物车里面是否是有对应的菜品或者套餐的
        List<ShoppingCart> shoplist = shoppingCartMapper.list(shoppingCart);
        if(shoplist != null && shoplist.size() > 0){ //购物车有数据
            //2.假设存在菜品/套餐的话  直接就数量加1即可
            //那么直接在购物车的数据里面进行数量加1
            //这里有个逻辑要搞清楚 查出来的数据只能是1个 如果购物车有两个是相同的话 它早就得是数量2了
            ShoppingCart cart = shoplist.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }else{
            //3.假设不存在的话 就要往这个购物车表里面插入数据了
            //要判断是菜品还是套餐
            if(shoppingCart.getDishId() != null){ //假设它是菜品
                //根据菜品id查询菜品数据
                Dish dish = dishMapper.getByid(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else if(shoppingCart.getSetmealId() != null){
                //假设它是套餐
                Setmeal setdish = setDishMapper.getByid(shoppingCart.getSetmealId());
                shoppingCart.setName(setdish.getName());
                shoppingCart.setImage(setdish.getImage());
                shoppingCart.setAmount(setdish.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 获取购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean() {

        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //1.首先要查询这个购物车的这个商品信息
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list != null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            //2.假设这个商品数量为1 那么直接删除这个商品
            if(cart.getNumber() == 1){
                shoppingCartMapper.deleteAll(cart.getUserId());
            }else if(cart.getNumber() > 1){
                cart.setNumber(cart.getNumber() - 1);
                //3.假设这个商品数量大于1 那么数量减1
                shoppingCartMapper.update(cart);
            }
        }
    }
}
